package com.utc.cards.table.jade.agent.rulesAgent;

import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.utc.cards.common.jade.Mapper;
import com.utc.cards.model.deck.Deck;
import com.utc.cards.model.game.IRules;
import com.utc.cards.model.player.IPlayer;

public class SendAdviceBehaviour extends OneShotBehaviour
{

    private static final long serialVersionUID = 8878391510046047807L;

    private static Logger log = LoggerFactory.getLogger(SendAdviceBehaviour.class);

    private RulesAgent agent;
    private Deck hand;
    private IPlayer player;
    private IRules rules;

    public SendAdviceBehaviour(RulesAgent rulesAgent, Deck deck, IPlayer player)
    {
        super(rulesAgent);
        this.agent = rulesAgent;
        this.hand = deck;
        this.player = player;
        this.rules = agent.getModel().getGame().getRules();
    }

    @Override
    public void action()
    {
        log.debug("Send Advice possibles cards ...");
        Deck deck = rules.conseilCoup(hand, player);
        ACLMessage message = new ACLMessage(ACLMessage.INFORM);
        message.setConversationId("advice");
        message.addReceiver(agent.getModel().getPlayersMap().get(player.getName()));
        try
        {
            message.setContent(Mapper.getObjectMapper().writeValueAsString(deck));
            agent.send(message);
            log.debug("Send Advice possibles cards DONE");
        }
        catch (JsonProcessingException e)
        {
            log.error("Error while ");
            e.printStackTrace();
        }
    }

}
